package com.mobiusws.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
	public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd HH24:mm:ss");
	public static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd");
	public static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat(
			"yyyy-MM");

	public static Date string2Date(String formatStr, String dateStr)
			throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
		return formatter.parse(dateStr);
	}

	public static Date string2Date(SimpleDateFormat formatter, String dateStr)
			throws Exception {
		return formatter.parse(dateStr);
	}

	public static String date2String(String formatStr, Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
		return formatter.format(date);
	}

	public static String date2String(SimpleDateFormat formatter, Date date) {
		return formatter.format(date);
	}
	
	public static String GMTCovert(String GMTSource, String GMTDestination, String strSourceTimestamp)
	{
		TimeZone swTimeZone = TimeZone.getTimeZone(GMTSource);

		SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		inFormat.setTimeZone(swTimeZone);

		Date parseDate = null;
		try
		{
			parseDate = inFormat.parse(strSourceTimestamp);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}

		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		outFormat.setTimeZone(TimeZone.getTimeZone(GMTDestination));
		String outPutStr = outFormat.format(parseDate);

		return outPutStr;
	}
	
	
	 public static String getNextDay(Date day) {
		 	Date preDate = null;
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(day);
	        cal.add(Calendar.DAY_OF_MONTH, 1);
	        
	        preDate = cal.getTime();
	        return date2String("yyyy-MM-dd HH:mm:ss",preDate);
	 }
	 
	 public static Date getPrev24Hour(Date day) {
		 	Date preDate = null;
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(day);
	        cal.add(Calendar.HOUR_OF_DAY, -24);
	        
	        preDate = cal.getTime();
	        return preDate;
	 }
	 
	 public static Date getNext1Hour(Date day) {
		 	Date preDate = null;
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(day);
	        cal.add(Calendar.HOUR_OF_DAY, 1);
	        
	        preDate = cal.getTime();
	        return preDate;
	 }
	 
	 public static String getNext1Sec(Date day) {
		 	Date preDate = null;
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(day);
	        cal.add(Calendar.SECOND, 1);
	        
	        preDate = cal.getTime();
	        return date2String("yyyy-MM-dd HH:mm:ss",preDate);
	 }
	 
	 
	 public static Date getAfterDate(Date date,int days)  
	 {  
	     //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
	     Calendar calendar = Calendar.getInstance();     
	     calendar.setTime(date);  
	     calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR) + days);  
	     return calendar.getTime();  
	 }  
}
